package com.ruoyi.quotation.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.quotation.domain.QuoPricing;
import com.ruoyi.quotation.domain.QuoQuote;
import com.ruoyi.quotation.mapper.QuoPricingMapper;
import com.ruoyi.quotation.mapper.QuoQuoteMapper;
import com.ruoyi.quotation.service.IQuoPricingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 成本核算服务实现
 */
@Service
public class QuoPricingServiceImpl implements IQuoPricingService {

    @Autowired
    private QuoPricingMapper quoPricingMapper;

    @Autowired
    private QuoQuoteMapper quoQuoteMapper;

    /**
     * 查询成本核算列表
     *
     * @param quoPricing 成本核算信息
     * @return 成本核算列表
     */
    @Override
    public List<QuoPricing> selectQuoPricingList(QuoPricing quoPricing) {
        return quoPricingMapper.selectQuoPricingList(quoPricing);
    }

    /**
     * 根据ID查询成本核算信息
     *
     * @param id 成本核算ID
     * @return 成本核算信息
     */
    @Override
    public QuoPricing selectQuoPricingById(Integer id) {
        return quoPricingMapper.selectQuoPricingById(id);
    }

    /**
     * 根据订单编号查询成本核算信息
     *
     * @param orderNo 订单编号
     * @return 成本核算信息
     */
    @Override
    public QuoPricing selectQuoPricingByOrderNo(String orderNo) {
        return quoPricingMapper.selectQuoPricingByOrderNo(orderNo);
    }

    /**
     * 新增成本核算
     *
     * @param quoPricing 成本核算信息
     * @return 结果
     */
    @Override
    public int insertQuoPricing(QuoPricing quoPricing) {
        quoPricing.setCreateBy(SecurityUtils.getUsername());
        quoPricing.setCreateTime(new Date());
        return quoPricingMapper.insertQuoPricing(quoPricing);
    }

    /**
     * 修改成本核算
     *
     * @param quoPricing 成本核算信息
     * @return 结果
     */
    @Override
    public int updateQuoPricing(QuoPricing quoPricing) {
        quoPricing.setUpdateBy(SecurityUtils.getUsername());
        quoPricing.setUpdateTime(new Date());
        return quoPricingMapper.updateQuoPricing(quoPricing);
    }

    /**
     * 批量删除成本核算
     *
     * @param ids 需要删除的成本核算ID
     * @return 结果
     */
    @Override
    public int deleteQuoPricingByIds(Integer[] ids) {
        return quoPricingMapper.deleteQuoPricingByIds(ids);
    }

    /**
     * 执行财务审核
     *
     * @param quoPricing 成本核算信息
     * @return 结果
     */
    @Override
    @Transactional
    public int auditPricing(QuoPricing quoPricing) {
        // 计算价格
        calculatePrice(quoPricing);
        
        // 1. 新增或更新成本核算信息
        QuoPricing exist = quoPricingMapper.selectQuoPricingByOrderNo(quoPricing.getOrderNo());
        int rows = 0;
        
        if (exist != null) {
            quoPricing.setNo(exist.getNo());
            quoPricing.setUpdateBy(SecurityUtils.getUsername());
            quoPricing.setUpdateTime(new Date());
            rows = quoPricingMapper.updateQuoPricing(quoPricing);
        } else {
            quoPricing.setCreateBy(SecurityUtils.getUsername());
            quoPricing.setCreateTime(new Date());
            rows = quoPricingMapper.insertQuoPricing(quoPricing);
        }
        
        // 2. 更新报价单状态为"总经理审核中"
        if (rows > 0) {
            QuoQuote quoQuote = new QuoQuote();
            quoQuote.setOrderNo(quoPricing.getOrderNo());
            quoQuote.setStatus("总经理审核中");
            quoQuote.setUpdateBy(SecurityUtils.getUsername());
            quoQuoteMapper.updateQuoteStatusByOrderNo(quoQuote);
        }
        
        return rows;
    }

    /**
     * 财务反审核
     *
     * @param quoPricing 成本核算信息
     * @return 结果
     */
    @Override
    @Transactional
    public int reauditPricing(QuoPricing quoPricing) {
        // 计算价格
        calculatePrice(quoPricing);
        
        // 1. 更新成本核算信息
        quoPricing.setUpdateBy(SecurityUtils.getUsername());
        quoPricing.setUpdateTime(new Date());
        int rows = quoPricingMapper.updateQuoPricing(quoPricing);
        
        // 2. 更新报价单状态为"财务反审核完成"
        if (rows > 0) {
            QuoQuote quoQuote = new QuoQuote();
            quoQuote.setOrderNo(quoPricing.getOrderNo());
            quoQuote.setStatus("财务反审核完成");
            if (quoPricing.getHsPrice() != null) {
                quoQuote.setHsPrice(new BigDecimal(quoPricing.getHsPrice()));
            }
            quoQuote.setUpdateBy(SecurityUtils.getUsername());
            quoQuoteMapper.updateQuoteStatusByOrderNo(quoQuote);
        }
        
        return rows;
    }

    /**
     * 计算价格
     *
     * @param quoPricing 成本核算信息
     * @return 计算后的成本核算信息
     */
    @Override
    public QuoPricing calculatePrice(QuoPricing quoPricing) {
        // 1. 计算费用总和
        double totalCost = 0.0;
        if (quoPricing.getMaterial() != null) totalCost += quoPricing.getMaterial();
        if (quoPricing.getExtrusion() != null) totalCost += quoPricing.getExtrusion();
        if (quoPricing.getBackend() != null) totalCost += quoPricing.getBackend();
        if (quoPricing.getOutsourcing() != null) totalCost += quoPricing.getOutsourcing();
        if (quoPricing.getAssemble() != null) totalCost += quoPricing.getAssemble();
        if (quoPricing.getInspection() != null) totalCost += quoPricing.getInspection();
        if (quoPricing.getWrap() != null) totalCost += quoPricing.getWrap();
        if (quoPricing.getTransport() != null) totalCost += quoPricing.getTransport();
        if (quoPricing.getSale() != null) totalCost += quoPricing.getSale();
        if (quoPricing.getManage() != null) totalCost += quoPricing.getManage();
        if (quoPricing.getAd() != null) totalCost += quoPricing.getAd();
        
        // 2. 计算未税单价
        double profitMargin = quoPricing.getProfitmargin() != null ? quoPricing.getProfitmargin() : 0.0;
        double wsPrice = 0.0;
        if (profitMargin < 1) {
            wsPrice = totalCost / (1 - profitMargin);
        }
        
        // 3. 计算含税单价
        double rate = quoPricing.getRate() != null ? quoPricing.getRate() : 0.0;
        double hsPrice = wsPrice * (1 + rate);
        
        // 4. 设置计算结果
        quoPricing.setWsPrice(wsPrice);
        quoPricing.setHsPrice(hsPrice);
        
        return quoPricing;
    }
} 